<template>
  <el-card>
    <el-form :inline="true" :disabled="disabled">
      <el-form-item label="一级分类">
        <!-- 
          v-model给组件绑定默认做了两件事：
            modelValue
            update:modelValue
          -->
        <el-select
          placeholder="请选择一级分类"
          :modelValue="category.category1Id"
          @update:modelValue="category1IdChange"
        >
          <!-- 
            label 是显示的文字
            value 是选中时得到的值

            :value="c1.id" 
              因为将来请求二级分类需要一级分类的id
              value的值为id，就能获取到id
           -->
          <el-option
            v-for="c1 in category.category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
          <!-- <el-option label="Zone two" value="beijing" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择二级分类"
          :modelValue="category.category2Id"
          @update:modelValue="category2IdChange"
        >
          <el-option
            v-for="c2 in category.category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择三级分类" v-model="category.category3Id">
          <el-option
            v-for="c3 in category.category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySeletor",
};
</script>

<script lang="ts" setup>
import { onMounted, onBeforeMount } from "vue";
// import type { Category } from "./model";
import { useCategoryStore } from "@/stores/category";
// const category = reactive<Category>({
//   category1Id: undefined,
//   category2Id: undefined,
//   category3Id: undefined,
//   category1List: [],
//   category2List: [],
//   category3List: [],
// });

defineProps<{ disabled: boolean }>();

const category = useCategoryStore();
// 请求一级分类列表
onMounted(async () => {
  category.getCategory1List();
});
// 当一级分类id发送变化触发的函数
const category1IdChange = async (category1Id: number) => {
  // 请求二级分类列表
  category.getCategory2List(category1Id);
  //   // 更新一级分类id
  //   category.category1Id = category1Id;
  //   category.category2Id = undefined;
  //   category.category3Id = undefined;
  //   // 清空三级分类列表
  //   category.category3List = [];
};
// 当二级分类id发送变化触发的函数
const category2IdChange = async (category2Id: number) => {
  category.getCategory3List(category2Id);
  //   category.category2Id = category2Id;
  //   category.category3Id = undefined;
};

onBeforeMount(() => {
  category.category1Id = undefined;
  category.category2Id = undefined;
  category.category3Id = undefined;
  category.category1List = [];
  category.category2List = [];
  category.category3List = [];
});
</script>

<style scoped>
.el-form-item {
  margin: 20px 10px;
}
</style>
